@ts-nocheck
<template>
  <!-- :class="['wrapper-default', capacityStore.selectedRecordStatus == 1 ? 'wrapper-effictive' : (capacityStore.selectedRecordStatus == 0 ? 'wrapper-ineffictive' : (capacityStore.selectedRecordStatus == -1 ? 'wrapper-expired' : ''))]" -->
  <div
    v-if="selectedContent.length == graphics.areas.length"
    :class="[
      'wrapper-default',
      capacityStore.selectedRecordStatus == 1
        ? 'wrapper-effictive'
        : capacityStore.selectedRecordStatus == 0
        ? 'wrapper-ineffictive'
        : capacityStore.selectedRecordStatus == -1
        ? 'wrapper-expired'
        : '',
    ]"
  >
    <svg
      xmlns="http://www.w3.org/2000/svg"
      width="100%"
      height="100%"
      viewBox="0 0 841.31 940.91"
      v-if="graphics"
    >
      <g id="gatepoints">
        <text
          v-for="label in graphics.gatepoints"
          :transform="getTranslate(label.position)"
          :class="['gatepoint-default', darkMode ? 'gatepoint-darkmode' : '']"
        >
          {{ label.id }}
        </text>
      </g>
      <g id="boundary" v-if="false">
        <polygon
          v-for="polygon in graphics.boundary"
          :points="polygon.points.join(' ')"
          :class="['boundary-default', darkMode ? 'boundary-darkmode' : '']"
        ></polygon>
      </g>
      <g id="areas">
        <!-- @dblclick="$emit('clickConfig', { id: area.id, idx: index })" -->
        <g
          v-for="(area, index) in graphics.areas"
          @mouseenter="hoveredItem = area.id"
          @mouseleave="hoveredItem = -1"
          @click="$emit('hover', index)"
          @contextmenu.prevent.stop="
            $emit('clickConfig', { id: area.id, idx: index })
          "
        >
          <polygon
            :points="area.points"
            :class="[
              'area-default',
              darkMode ? 'area-darkmode' : '',
              hoveredItem == area.id ? 'area-hover' : '',
              selectedContent[index].capacityRatio >= 0.9
                ? 'area-high'
                : selectedContent[index].capacityRatio >= 0.7
                ? 'area-middle'
                : 'area-low',
            ]"
          ></polygon>
          <g
            :class="['tag-default', hoveredItem == area.id ? 'tag-hover' : '']"
          >
            <text
              class="tag-large"
              :transform="getTranslate(area.position, [0, 0])"
            >
              {{ selectedContent[index].areaName }}:&nbsp;{{
                selectedContent[index].capacityRatio * 100
              }}%
            </text>

            <text
              class="tag-large"
              :transform="getTranslate(area.position, [0, 30])"
            >
              {{ selectedContent[index].currentCapacity }}架/时
            </text>
          </g>
        </g>
      </g>
      <g id="totalCapacity">
        <text
          :class="[
            'total-capacity-default',
            darkMode ? 'total-capacity-darkmode' : '',
          ]"
          transform="translate(0,30)"
        >
          通行能力{{ capacityStore.computedTotalCapacity().value }}架/时
        </text>
      </g>
    </svg>
    <!-- <pre>{{graphics}}</pre> -->
  </div>
</template>

<script setup>
import { ref, computed, onMounted, nextTick } from "vue";
import useCommonStore from "@/store/common";
import useCapacityStore from "@/store/capacity";
import graphicsRaw from "./graphics.json";

const emit = defineEmits(["hover", "clickConfig"]);
const capacityStore = useCapacityStore();
const commonStore = useCommonStore();
const graphics = ref(graphicsRaw);
const darkMode = computed(() => commonStore.darkMode);

const selectedRecord = computed(() => capacityStore.selectedRecord);
const selectedContent = computed(() => selectedRecord.value.content);

const getTranslate = (
  [translateX, translateY],
  [offsetX, offsetY] = [0, 0]
) => {
  return (
    "translate(" + (translateX + offsetX) + " " + (translateY + offsetY) + ")"
  );
};

const hoveredItem = ref(-1);
const handleHover = (index) => {
  // if (hoveredItem.value != -1) return;
  // hoveredItem.value = index;
  // emit('hover', index);
};
</script>

<style scoped>
svg {
  user-select: none;
}

.wrapper-default {
  width: 100%;
  height: 100%;
  padding: 12px;
  border-radius: 6px;
  transition: all 1s ease-in-out;
  box-shadow: none;
}

.wrapper-effictive {
  box-shadow: rgba(103, 194, 58, 0.3) 0px 0px 44px inset;
  background: radial-gradient(
    circle,
    rgba(103, 194, 58, 0.1) 30%,
    rgba(103, 194, 58, 0.3) 100%
  );
  animation: effective 10s ease-in-out infinite;
}

.wrapper-ineffictive {
  box-shadow: rgba(64, 158, 255, 0.15) 0px 0px 22px inset;
  background: radial-gradient(
    circle,
    rgba(64, 158, 255, 0.05) 60%,
    rgba(64, 158, 255, 0.15) 100%
  );
}

.wrapper-expired {
  box-shadow: rgba(144, 147, 153, 0.15) 0px 0px 22px inset;
  background: radial-gradient(
    circle,
    rgba(144, 147, 153, 0.05) 60%,
    rgba(144, 147, 153, 0.15) 100%
  );
}

@keyframes effective {
  0% {
    background-position: 0 0;
  }

  15% {
    background-position: -44px 0px;
  }

  20% {
    background-position: -44px -44px;
  }

  30% {
    background-position: 44px -44px;
  }

  45% {
    background-position: 44px 0px;
  }

  50% {
    background-position: 0px 0px;
  }

  60% {
    background-position: -44px 0px;
  }

  75% {
    background-position: -44px 44px;
  }

  80% {
    background-position: 44px 44px;
  }

  95% {
    background-position: 44px 0px;
  }

  100% {
    background-position: 0 0;
  }
}

.area-default {
  fill: #909399;
  stroke: #000;
  stroke-miterlimit: 10;
  stroke-width: 1;
  transition: all 0.33s ease-in-out;
  opacity: 0.75;
  cursor: pointer;
  /* stroke-dasharray: 2; */
}

.area-hover {
  opacity: 1;
  animation-name: none;
  stroke-width: 5;
  /* stroke-dasharray: 5; */
  /* stroke-dasharray: 0; */
}

.area-high {
  fill: #67c23a;
}

.area-middle {
  fill: #e6a23c;
}

.area-low {
  fill: #f56c6c;
}

.area-darkmode {
  stroke: #fff;
}

.boundary-default {
  fill: none;
  stroke: #000;
  stroke-miterlimit: 10;
  stroke-width: 1;
  /* stroke-dasharray: 1; */
}

.boundary-darkmode {
  stroke: #fff;
}

.gatepoint-default {
  fill: #000;
  font-size: 18px;
  font-family: Consolas;
}

.gatepoint-darkmode {
  fill: #fff;
}

.tag-default {
  fill: #fff;
  cursor: pointer;
  transition: all 0.5s ease-in-out;
  text-shadow: 0px 1px 4px rgba(0, 0, 0, 0.33);
  opacity: 0.85;
}

.tag-small {
  /* font-weight: bold; */
  font-size: 16px;
}

.tag-large {
  /* font-family: Consolas; */
  font-weight: bold;
  font-size: 24px;
}

.tag-hover {
  /* font-size: 28px; */
  /* font-weight: bold; */
  text-shadow: 0px 2px 8px rgba(0, 0, 0, 1);
  opacity: 1;
}

.area-perfect {
  fill: #409eff;
}

.area-fine {
  fill: rgba(103, 194, 58, 0.5);
}

.area-warning {
  fill: #e6a23c;
}

.area-danger {
  fill: #f56c6c;
}

.total-capacity-default {
  fill: #000;
  font-size: 24px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.5s ease-in-out;
  text-shadow: 0px 1px 3px rgba(0, 0, 0, 0.25);
  opacity: 0.75;
}

.total-capacity-default:hover {
  text-shadow: 0px 2px 6px rgba(0, 0, 0, 0.5);
  opacity: 1;
}

.total-capacity-darkmode {
  fill: #fff;
}

.area-light {
  animation-name: light;
  animation-duration: 0.75s;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

@keyframes light {
  from {
    opacity: 1;
  }

  to {
    opacity: 0.75;
  }
}

@keyframes strokeanim {
  to {
    stroke-dashoffset: 0;
  }
}
</style>
